﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using YArchitech.LIB.YJKControls;
using YJKArchMethodLibrary.Utils;
using YJKFamilyLibDataManager;

namespace GlobleSetModule
{
	public partial class DlgProSet : System.Windows.Forms.Form
	{
		public DlgProSet()
		{
			this.InitializeComponent();
		}

		public DlgProSet(Document doc, FamilyLibDatabase adb, FamilyLibDatabase hdb, FamilyLibDatabase edb, FamilyLibDatabase pdb, FamilyLibDatabase sdb)
		{
			this.m_document = doc;
			this.hvacDB = hdb;
			this.archDB = adb;
			this.elecDB = edb;
			this.plumDB = pdb;
			this.sysDB = sdb;
			this.fInfo = Assist.FileUtil.GetProjConfigFile(this.m_document);
			this.InitializeComponent();
			this.InitCmbGridType();
			this.InitComboColFamily();
			this.InitComboDoorFamily();
			this.InitComboWinFamily();
			this.InitCmbLineDimType();
			this.InitCmbArcDimType();
			this.InitCmbDoorTag();
			this.InitCmbWinTag();
			this.btnAppliance.Enabled = false;
		}

		private void InitCmbGridType()
		{
			List<GridType> gridTypes = Common.GetGridTypes(this.m_document);
			foreach (Element element in gridTypes)
			{
				GridType gridType = element as GridType;
				this.cmbDefaultAxis.Items.Add(gridType.Name.Trim());
			}
			if (this.cmbDefaultAxis.Items.Count > 0)
			{
				string text = Assist.FileUtil.GetDataFromConfigFile(this.fInfo.FullName, "全局配置", "轴线样式").Trim();
				if (this.cmbDefaultAxis.Items.Contains(text))
				{
					this.cmbDefaultAxis.SelectedItem = text;
					return;
				}
				this.cmbDefaultAxis.SelectedIndex = 0;
			}
		}

		public void InitComboColFamily()
		{
			DbCategory dbCategory = this.archDB.CategoryTable["结构柱"];
			if (dbCategory == null)
			{
				return;
			}
			List<DbFamily> allFamilies = dbCategory.GetAllFamilies(false, CheckType.All);
			foreach (DbFamily dbFamily in allFamilies)
			{
				this.cmbDefaultCol.Items.Add(dbFamily.Name.Trim());
			}
			if (this.cmbDefaultCol.Items.Count > 0)
			{
				string text = Assist.FileUtil.GetDataFromConfigFile(this.fInfo.FullName, "全局配置", "柱样式").Trim();
				if (this.cmbDefaultCol.Items.Contains(text))
				{
					this.cmbDefaultCol.SelectedItem = text;
					return;
				}
				this.cmbDefaultCol.SelectedIndex = 0;
			}
		}

		private void InitComboDoorFamily()
		{
			DbCategory dbCategory = this.archDB.CategoryTable["门"];
			if (dbCategory == null)
			{
				return;
			}
			List<DbFamily> allFamilies = dbCategory.GetAllFamilies(false, CheckType.All);
			foreach (DbFamily dbFamily in allFamilies)
			{
				this.cmbDefaultDoor.Items.Add(dbFamily.Name.Trim());
			}
			if (this.cmbDefaultDoor.Items.Count > 0)
			{
				string text = Assist.FileUtil.GetDataFromConfigFile(this.fInfo.FullName, "全局配置", "门样式").Trim();
				if (this.cmbDefaultDoor.Items.Contains(text))
				{
					this.cmbDefaultDoor.SelectedItem = text;
					return;
				}
				this.cmbDefaultDoor.SelectedIndex = 0;
			}
		}

		private void InitComboWinFamily()
		{
			DbCategory dbCategory = this.archDB.CategoryTable["窗"];
			if (dbCategory == null)
			{
				return;
			}
			List<DbFamily> allFamilies = dbCategory.GetAllFamilies(false, CheckType.All);
			foreach (DbFamily dbFamily in allFamilies)
			{
				this.cmbDefaultWin.Items.Add(dbFamily.Name.Trim());
			}
			if (this.cmbDefaultWin.Items.Count > 0)
			{
				string text = Assist.FileUtil.GetDataFromConfigFile(this.fInfo.FullName, "全局配置", "窗样式").Trim();
				if (this.cmbDefaultWin.Items.Contains(text))
				{
					this.cmbDefaultWin.SelectedItem = text;
					return;
				}
				this.cmbDefaultWin.SelectedIndex = 0;
			}
		}

		private void InitCmbLineDimType()
		{
			List<DimensionType> dimensionTypes = Common.GetDimensionTypes(this.m_document);
			foreach (Element element in dimensionTypes)
			{
				DimensionType dimensionType = element as DimensionType;
				if (dimensionType.StyleType == null && !this.cmbLineDim.Items.Contains(dimensionType.Name.Trim()))
				{
					this.cmbLineDim.Items.Add(dimensionType.Name.Trim());
				}
			}
			if (this.cmbLineDim.Items.Count > 0)
			{
				string text = Assist.FileUtil.GetDataFromConfigFile(this.fInfo.FullName, "全局配置", "直线标注").Trim();
				if (this.cmbLineDim.Items.Contains(text))
				{
					this.cmbLineDim.SelectedItem = text;
					return;
				}
				this.cmbLineDim.SelectedIndex = 0;
			}
		}

		private void InitCmbArcDimType()
		{
			List<DimensionType> dimensionTypes = Common.GetDimensionTypes(this.m_document);
			foreach (Element element in dimensionTypes)
			{
				DimensionType dimensionType = element as DimensionType;
                if ((int)dimensionType.StyleType == 1)
				{
					this.cmbArcDim.Items.Add(dimensionType.Name.Trim());
				}
			}
			if (this.cmbArcDim.Items.Count > 0)
			{
				string text = Assist.FileUtil.GetDataFromConfigFile(this.fInfo.FullName, "全局配置", "弧线标注").Trim();
				if (this.cmbArcDim.Items.Contains(text))
				{
					this.cmbArcDim.SelectedItem = text;
					return;
				}
				this.cmbArcDim.SelectedIndex = 0;
			}
		}

		private void InitCmbDoorTag()
		{
			DbCategory dbCategory = this.sysDB.CategoryTable["门标记"];
			if (dbCategory == null)
			{
				return;
			}
			List<DbFamily> allFamilies = dbCategory.GetAllFamilies(false, CheckType.All);
			foreach (DbFamily dbFamily in allFamilies)
			{
				this.cmbDoorTag.Items.Add(dbFamily.Name);
			}
			if (this.cmbDoorTag.Items.Count > 0)
			{
				string dataFromConfigFile = Assist.FileUtil.GetDataFromConfigFile(this.fInfo.FullName, "全局配置", "门标注");
				if (this.cmbDoorTag.Items.Contains(dataFromConfigFile))
				{
					this.cmbDoorTag.SelectedItem = dataFromConfigFile;
					return;
				}
				this.cmbDoorTag.SelectedIndex = 0;
			}
		}

		private void InitCmbWinTag()
		{
			DbCategory dbCategory = this.sysDB.CategoryTable["窗标记"];
			if (dbCategory == null)
			{
				return;
			}
			List<DbFamily> allFamilies = dbCategory.GetAllFamilies(false, CheckType.All);
			foreach (DbFamily dbFamily in allFamilies)
			{
				this.cmbWinTag.Items.Add(dbFamily.Name);
			}
			if (this.cmbWinTag.Items.Count > 0)
			{
				string dataFromConfigFile = Assist.FileUtil.GetDataFromConfigFile(this.fInfo.FullName, "全局配置", "窗标注");
				if (this.cmbWinTag.Items.Contains(dataFromConfigFile))
				{
					this.cmbWinTag.SelectedItem = dataFromConfigFile;
					return;
				}
				this.cmbWinTag.SelectedIndex = 0;
			}
		}

		private void btnOk_Click(object sender, EventArgs e)
		{
			try
			{
				Assist.FileUtil.WriteGlobleConfig(this.fInfo.FullName, this.getConfigDict());
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "盈建科提示");
			}
		}

		private Dictionary<string, string> getConfigDict()
		{
			Dictionary<string, string> dictionary = new Dictionary<string, string>();
			dictionary.Add("用户目录", this.tbUserDir.Text);
			foreach (object obj in base.Controls)
			{
                System.Windows.Forms.Control control = (System.Windows.Forms.Control)obj;
				if (control is GroupBox)
				{
					foreach (object obj2 in control.Controls)
					{
                        System.Windows.Forms.Control control2 = (System.Windows.Forms.Control)obj2;
						if (control2 is ComboBox && ((ComboBox)control2).SelectedItem != null)
						{
							dictionary.Add(control.Text, ((ComboBox)control2).SelectedItem.ToString());
						}
					}
				}
			}
			return dictionary;
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			if (this.btnAppliance.Enabled && MessageBox.Show("是否保存修改？", "盈建科提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
			{
				Assist.FileUtil.WriteGlobleConfig(this.fInfo.FullName, this.getConfigDict());
			}
		}

		private void cmbDefaultAxis_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.btnAppliance.Enabled = true;
		}

		private void cmbDefaultCol_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.btnAppliance.Enabled = true;
		}

		private void cmbDefaultDoor_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.btnAppliance.Enabled = true;
		}

		private void cmbDefaultWin_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.btnAppliance.Enabled = true;
		}

		private void cmbLineDim_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.btnAppliance.Enabled = true;
		}

		private void cmbArcDim_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.btnAppliance.Enabled = true;
		}

		private void cmbDoorTag_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.btnAppliance.Enabled = true;
		}

		private void cmbWinTag_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.btnAppliance.Enabled = true;
		}

		private void btnAppliance_Click(object sender, EventArgs e)
		{
			Assist.FileUtil.WriteGlobleConfig(this.fInfo.FullName, this.getConfigDict());
			this.btnAppliance.Enabled = false;
		}

		private void button2_Click(object sender, EventArgs e)
		{
			try
			{
				FileInfo tempDataFile = Assist.FileUtil.GetTempDataFile();
				string tempPath = Path.GetTempPath();
				FileInfo fileInfo = new FileInfo(tempPath + "ArcAxis.dat");
				FileInfo fileInfo2 = new FileInfo(tempPath + "RectAxis.dat");
				FileInfo fileInfo3 = new FileInfo(tempPath + "AxisToWall.dat");
				FileInfo fileInfo4 = new FileInfo(tempPath + "WallCorner.dat");
				FileInfo fileInfo5 = new FileInfo(tempPath + "LevelSet.dat");
				FileInfo fileInfo6 = new FileInfo(tempPath + "ColumnPlace.dat");
				fileInfo.Delete();
				fileInfo2.Delete();
				fileInfo3.Delete();
				fileInfo4.Delete();
				fileInfo5.Delete();
				fileInfo6.Delete();
				tempDataFile.Delete();
				Assist.FileUtil.CreateTempXmlFile(tempDataFile.FullName);
				this.cmbDefaultAxis.SelectedIndex = 0;
				this.cmbDefaultCol.SelectedIndex = 0;
				this.cmbDefaultDoor.SelectedIndex = 0;
				this.cmbDefaultWin.SelectedIndex = 0;
				this.cmbLineDim.SelectedIndex = 0;
				this.cmbArcDim.SelectedIndex = 0;
				this.cmbDoorTag.SelectedIndex = 0;
				this.cmbWinTag.SelectedIndex = 0;
				MessageBox.Show("临时数据已清空", "盈建科提示");
			}
			catch
			{
				MessageBox.Show("临时数据清空异常", "盈建科提示");
			}
		}

		private void SetWidth(ComboBox cbx)
		{
			int num = 0;
			Graphics graphics = base.CreateGraphics();
			for (int i = 0; i < cbx.Items.Count; i++)
			{
				cbx.SelectedIndex = i;
				SizeF sizeF = graphics.MeasureString(cbx.Text, cbx.Font);
				if (num < (int)sizeF.Width)
				{
					num = (int)sizeF.Width;
				}
			}
			cbx.DropDownWidth = cbx.Width;
			if (cbx.DropDownWidth < num)
			{
				cbx.DropDownWidth = num;
			}
		}

		private void button1_Click(object sender, EventArgs e)
		{
			foreach (object obj in base.Controls)
			{
                System.Windows.Forms.Control control = (System.Windows.Forms.Control)obj;
				if (control is GroupBox)
				{
					foreach (object obj2 in control.Controls)
					{
                        System.Windows.Forms.Control control2 = (System.Windows.Forms.Control)obj2;
						if (control2 is ComboBox && ((ComboBox)control2).Items.Count > 0)
						{
							((ComboBox)control2).SelectedIndex = 0;
						}
					}
				}
			}
		}

		private void cmbDefaultAxis_SelectedIndexChanged_1(object sender, EventArgs e)
		{
			this.btnAppliance.Enabled = true;
		}

		private void cmbDefaultCol_SelectedIndexChanged_1(object sender, EventArgs e)
		{
			this.btnAppliance.Enabled = true;
		}

		private void cmbDefaultDoor_SelectedIndexChanged_1(object sender, EventArgs e)
		{
			this.btnAppliance.Enabled = true;
		}

		private void cmbDefaultWin_SelectedIndexChanged_1(object sender, EventArgs e)
		{
			this.btnAppliance.Enabled = true;
		}

		private void btnUserDir_Click(object sender, EventArgs e)
		{
			using (FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog())
			{
				if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
				{
					this.tbUserDir.Text = folderBrowserDialog.SelectedPath;
				}
			}
		}

		private void DlgProSet_Load(object sender, EventArgs e)
		{
		}

		private Document m_document;

		public FamilyLibDatabase hvacDB;

		public FamilyLibDatabase archDB;

		public FamilyLibDatabase elecDB;

		public FamilyLibDatabase plumDB;

		public FamilyLibDatabase sysDB;

		private FileInfo fInfo;
	}
}
